package com.lzy.model;

import com.lzy.constant.ResultCodeEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author lzy by 2019/07/10
 */
@Data
@EqualsAndHashCode
public class RestResponseResult<T> {
    private static final int SUCCESS = 200;

    private static final int FAIL = 500;
    /**
     * 状态码
     */
    @ApiModelProperty(name = "code", dataType = "Integer", required = true, value = "状态码", notes = "全局状态码[200:成功]")
    private Integer code;
    /**
     * 消息
     */
    @ApiModelProperty(name = "message", dataType = "String", required = true, value = "消息")
    private String message;
    /**
     * 调用时间
     */
    @ApiModelProperty(name = "timestamp", dataType = "Long", required = true, value = "返回时间", example = "1569191782000")
    private Long timestamp = System.currentTimeMillis();
    /**
     * 数据
     */
    @ApiModelProperty(name = "data", required = true, value = "返回数据")
    private T data;
    /**
     * 请求id
     */
    @ApiModelProperty(name = "requestId", required = true, value = "请求id")
    private String requestId;


    public static <T> RestResponseResult<T> failResponse(Integer code, String message) {
        RestResponseResult<T> response = new RestResponseResult<>();
        response.setCode(code);
        response.setMessage(message);
        return response;
    }

    public static <T> RestResponseResult<T> failResponse(String message) {
        RestResponseResult<T> response = new RestResponseResult<>();
        response.setCode(FAIL);
        response.setMessage(message);
        return response;
    }

    public static <T> RestResponseResult<T> failResponse(Integer code, String message, T t) {
        RestResponseResult<T> response = new RestResponseResult<>();
        response.setCode(code);
        response.setMessage(message);
        response.setData(t);
        return response;
    }

    public static <T> RestResponseResult<T> failResponse(ResultCodeEnum codeEnum) {
        RestResponseResult<T> response = new RestResponseResult<>();
        response.setCode(codeEnum.getCode());
        response.setMessage(codeEnum.getText());
        return response;
    }

    public static <T> RestResponseResult<T> failResponse(ResultCodeEnum codeEnum, T t) {
        RestResponseResult<T> response = new RestResponseResult<>();
        response.setCode(codeEnum.getCode());
        response.setMessage(codeEnum.getText());
        response.setData(t);
        return response;
    }

    public static <T> RestResponseResult<T> success() {
        RestResponseResult<T> response = new RestResponseResult<>();
        response.setCode(SUCCESS);
        return response;
    }

    public static <T> RestResponseResult<T> success(T t) {
        RestResponseResult<T> response = new RestResponseResult<>();
        response.setCode(SUCCESS);
        response.setData(t);
        return response;
    }


    public boolean isSuccess() {
        return this.code.compareTo(SUCCESS) == 0;
    }

    public boolean isFail() {
        return !isSuccess();
    }


}
